package com.itliang.web.controller.hardware;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itliang.annotation.Log;
import com.itliang.core.model.dto.equipment.StatisticsQueryRequest;
import com.itliang.core.model.entity.equipment.Statistics;
import com.itliang.core.result.BaseResponse;
import com.itliang.core.result.CodeEnum;
import com.itliang.core.result.ResultUtils;
import com.itliang.enums.BusinessType;
import com.itliang.exception.BusinessException;
import com.itliang.service.hardware.EquipmentDataService;
import com.itliang.service.hardware.StatisticsService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author : liangzhuoyuan
 */
@Api(tags = "统计数据管理接口")
@RestController
@RequestMapping("/admin/hardware/statistics")
@Slf4j
public class StatisticsController {

    @Resource
    private StatisticsService statisticsService;

    @Resource
    private EquipmentDataService equipmentDataService;


    /**
     * @param statistics
     * @return
     */
    @PostMapping("/get/data")
    public BaseResponse<Statistics> getStatisticsData(@RequestBody Statistics statistics) {
        if (statistics.getDataType() == null) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        Statistics statisticsData = equipmentDataService.getStatisticsData(statistics);
        log.info(statistics.toString());
        return ResultUtils.success(statisticsData);

    }

    // region 增删改查

    /**
     * 创建统计数据
     *
     * @return
     */
    @Log(title = "统计数据管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public BaseResponse<Long> addStatistics(@RequestBody Statistics statistics) {
        statisticsService.addStatisticsData(statistics);
        return ResultUtils.success("恭喜，添加操作成功！");
    }

    /**
     * 删除统计数据
     *
     * @param id 统计数据id
     * @return
     */
    @Log(title = "统计数据管理", businessType = BusinessType.DELETE)
    @GetMapping("/delete")
    public BaseResponse<Boolean> deleteStatistics(int id) {
        boolean b = statisticsService.removeById(id);
        return ResultUtils.success(b, "恭喜，删除操作成功！");
    }

    /**
     * 批量删除统计数据
     *
     * @param idList id集合
     * @return
     */
    @Log(title = "统计数据管理", businessType = BusinessType.BATCHDELETE)
    @PostMapping("/delete/batch")
    public BaseResponse<Boolean> batchDeleteStatistics(@RequestBody List<Integer> idList) {
        boolean b = statisticsService.removeBatchByIds(idList);
        return ResultUtils.success(b, "恭喜，批量删除操作成功！");
    }

    /**
     * 更新统计数据
     *
     * @param statistics 统计数据信息
     * @return
     */
    @Log(title = "统计数据管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public BaseResponse<Boolean> updateStatistics(@RequestBody Statistics statistics) {
        boolean result = statisticsService.updateById(statistics);
        return ResultUtils.success(result, "恭喜，修改操作成功！");
    }

    /**
     * 根据 id 获取统计数据
     *
     * @param id 统计数据id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<Statistics> getStatisticsById(Integer id) {
        if (id <= 0) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        Statistics statistics = statisticsService.getById(id);
        return ResultUtils.success(statistics);
    }

    /**
     * 获取统计数据列表
     *
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<Statistics>> listStatistics() {
        List<Statistics> list = statisticsService.list();
        return ResultUtils.success(list);
    }


    /**
     * 分页获取统计数据数据列表
     *
     * @param statisticsQueryRequest 条件参数
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<Statistics>> listStatisticsByPage(StatisticsQueryRequest statisticsQueryRequest) {
        long current = statisticsQueryRequest.getCurrent();
        long size = statisticsQueryRequest.getPageSize();

        QueryWrapper<Statistics> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(statisticsQueryRequest.getCode()), "code", statisticsQueryRequest.getCode());
        Page<Statistics> page = statisticsService.page(new Page<>(current, size), wrapper);
        return ResultUtils.success(page);
    }

    // endregion
}
